﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WpfCourse.Common;
using WpfCourse.Model;

namespace WpfCourse.ViewModel
{
    public class MainViewModel : NotifyBase
    {
        public UserModel LoginUserInfo { get; set; }

        private string _searchText;

        public string SearchText
        {
            get { return _searchText; }
            set { _searchText = value; this.DoNotify(); }
        }

        private FrameworkElement _mainContent;

        public FrameworkElement MainContent
        {
            get { return _mainContent; }
            set { _mainContent = value; this.DoNotify(); }
        }

        public CommandBase NavChangeCommand { get; set; }

        public MainViewModel()
        {
            // 默认显示首页
            this.DoNavChange("HomePage");
            this.NavChangeCommand = new CommandBase();
            this.NavChangeCommand.DoCanExecute = new Func<object,bool>( o => true);
            this.NavChangeCommand.DoExecute = new Action<object>(pageName => {
                DoNavChange(pageName);
            });
        }

        private void DoNavChange(object pageName)
        {
            Type pageType = Type.GetType("WpfCourse.View.MainPages." + pageName.ToString());
            ConstructorInfo construct = pageType.GetConstructor(System.Type.EmptyTypes);
            this.MainContent = (FrameworkElement)construct.Invoke(null);
        }
    }
}
